package com.bawei.rk_01.xq

import androidx.lifecycle.viewModelScope
import com.bawei.base.utils.SingLiveData
import com.bawei.base.utils.bean.AddCarEntity
import com.bawei.base.utils.bean.CouponsEntity
import com.bawei.base.utils.bean.GoodEnttiy
import com.bawei.base.utils.bean.Res
import com.bawei.base.utils.bean.XqEntity
import com.bawei.base.utils.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class XqViewModel : BaseViewModel() {

    val xqsuccess = SingLiveData<Res<XqEntity>>()
    val xqfail = SingLiveData<String>()

    fun goodsxu(id:Int,type:String){
        viewModelScope.launch {
            try {
                xqsuccess.postValue(apiService.goodsxq(id, type))
            }catch (e:Exception){
                xqfail.postValue(e.message.toString())
            }
        }
    }

    //渲染优惠卷
    val couponssuccess = SingLiveData<Res<CouponsEntity>>()
    val couponsfail = SingLiveData<String>()
    fun getcoupons(limit:Int, page:Int, type:Int){
        viewModelScope.launch {
            try {
                couponssuccess.postValue(apiService.getcoupons(limit, page, type))
            }catch (e:Exception){
                couponsfail.postValue(e.message.toString())
            }
        }
    }

    //优品推进
    val ypsuccess = SingLiveData<Res<GoodEnttiy>>()
    val ypfail = SingLiveData<String>()
    fun goodsYp(){
        viewModelScope.launch {
            try {
                ypsuccess.postValue(apiService.goodsYp())
            }catch (e:Exception){
                ypfail.postValue(e.message.toString())
            }
        }
    }

    //收藏
    val sharesuccess = SingLiveData<Res<String>>()
    val sharefail = SingLiveData<String>()
    fun share(category:String,id:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["category"] = category
                map["id"] = id
                sharesuccess.postValue(apiService.share(mapToBody(map)))
            }catch (e:Exception){
                sharefail.postValue(e.message.toString())
            }
        }
    }

    //取消收藏
    val nosharesuccess = SingLiveData<Res<String>>()
    val nosharefail = SingLiveData<String>()
    fun noshare(proId:Int){
        viewModelScope.launch {
            try {
                nosharesuccess.postValue(apiService.noshare(proId))
            }catch (e:Exception){
                nosharefail.postValue(e.message.toString())
            }
        }
    }

    //添加购物车
    val addcarsuccess = SingLiveData<Res<AddCarEntity>>()
    val addcarfail = SingLiveData<String>()
    fun addcar(cartNum:Int,productAttrUnique:String,productId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["cartNum"] = cartNum
                map["productAttrUnique"] = productAttrUnique
                map["productId"] = productId
                addcarsuccess.postValue(apiService.addcar(mapToBody(map)))
            }catch (e:Exception){
                addcarfail.postValue(e.message.toString())
            }
        }
    }

}